package dao

import (
	"Junior_two/subject/order/orderrpc/model"
	"fmt"
	"github.com/astaxie/beego/orm"
)

func ReadUserId(userId int64) bool {
	o := orm.NewOrm()
	u := model.Users{
		Id: userId,
	}
	fmt.Println(1111)
	err := o.Read(&u, "id")
	if err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

func ReadUGoodId(goodId int64) bool {
	o := orm.NewOrm()
	u := model.Goods{
		Id: goodId,
	}
	err := o.Read(&u, "id")
	if err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

func GetDataByGoodId(goodId int64) model.Goods {
	o := orm.NewOrm()
	g := model.Goods{
		Id: goodId,
	}
	err := o.Read(&g, "id")
	if err != nil {
		fmt.Println(err)
		return model.Goods{}
	}
	return g
}

func GetDataByUserId(userID int64) model.Users {
	o := orm.NewOrm()
	u := model.Users{
		Id: userID,
	}
	err := o.Read(&u, "id")
	if err != nil {
		fmt.Println(err)
		return model.Users{}
	}
	return u
}

func InsertOrder(o *model.Order) bool {
	db := orm.NewOrm()
	_, err := db.Insert(o)
	if err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

// GetDataByOrderId 根据订单编号获取信息
func GetDataByOrderId(orderNum int64) model.Order {
	db := orm.NewOrm()
	o := model.Order{
		OrderNum: orderNum,
	}
	err := db.Read(&o, "order_num")
	if err != nil {
		fmt.Println(err)
		return model.Order{}
	}
	return o
}
